import 'dart:async';

import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:rc_widget/rc_widget.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_study/widgets/public/themes/index.dart';

enum AppMediaType {
  photo,
  video,
}

class AppMediaPicker {
  AppMediaPicker({this.source = ImageSource.gallery}) {
    _showPicker();
  }
  final ImageSource source;
  final Completer<AppMediaResult> _completer = Completer();

  Future<void> _showPicker() async {
    final reslut = await Get.bottomSheet(Container(
      height: 300.h,
      decoration: BoxDecoration(
        color: AppThemes.of().colors.background1,
        borderRadius: BorderRadius.vertical(top: Radius.circular((30.sp))),
      ),
      child: Column(
        children: [
          _buildIconButton(
              title: '图片'.tr,
              type: AppMediaType.photo,
              icon: Icons.collections),
          _buildIconButton(
              title: '视频'.tr,
              type: AppMediaType.video,
              icon: Icons.video_library),
          _buildTextButton(),
        ],
      ),
    ));

    if (reslut == null && !_completer.isCompleted) {
      _completer.complete(AppMediaResult.fail());
    }
  }

  Widget _buildIconButton(
      {required String title,
      required AppMediaType type,
      required IconData icon}) {
    return RcGestureDetector(
      onTap: () {
        if (type == AppMediaType.video) {
          _getVideoFile();
        } else {
          _getImageFile();
        }

        Get.back(result: true);
      },
      child: Container(
        height: 100.h,
        decoration: AppThemes.of().bottomBorder1,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Icon(
              icon,
              size: 46.sp,
              color: AppThemes.of().colors.text1,
            ),
            SizedBox(
              width: 28.w,
            ),
            Text(
              title,
              style: AppThemes.of().w700Text128,
            )
          ],
        ),
      ),
    );
  }

  Widget _buildTextButton() {
    return RcGestureDetector(
      onTap: () => Get.back(),
      child: Container(
        alignment: Alignment.center,
        height: 100.h,
        child: Text(
          '取消'.tr,
          style: AppThemes.of().w400Text228,
        ),
      ),
    );
  }
  /// 获取视频文件
  Future<void> _getVideoFile() async {
    final ImagePicker picker = ImagePicker();

    final XFile? xFile = await picker.pickVideo(source: source);
    if (xFile != null) {
      _completer.complete(
        AppMediaResult(
          isSuccess: true,
          path: xFile.path,
          type: AppMediaType.video,
        ),
      );
    } else {
      _completer.complete(AppMediaResult.fail());
    }
  }
  /// 获取图片文件
  Future<void> _getImageFile() async {
    final ImagePicker picker = ImagePicker();

    final XFile? xFile = await picker.pickImage(source: source);

    if (xFile != null) {
      _completer.complete(
        AppMediaResult(
          isSuccess: true,
          path: xFile.path,
          type: AppMediaType.photo,
        ),
      );
    } else {
      _completer.complete(AppMediaResult.fail());
    }
  }

  /// 获取上传结果
  Future<AppMediaResult> asyncResult() async {
    return _completer.future;
  }
}

class AppMediaResult {
  AppMediaResult({
    this.path = '',
    this.isSuccess = false,
    this.type = AppMediaType.photo,
  });
  factory AppMediaResult.fail() => AppMediaResult();

  /// 文件路径
  final String path;

  final bool isSuccess;

  final AppMediaType type;
}
